package com.pk.platform.business.question.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;

import com.pk.platform.business.question.dao.IPaperDefineDao;
import com.pk.platform.business.question.dao.IPaperUseDao;
import com.pk.platform.business.question.service.IPaperUseService;
import com.pk.platform.business.question.vo.PaperVO;
import com.pk.platform.domain.question.Paper;
import com.pk.platform.domain.question.PaperFix;
import com.pk.platform.domain.question.Question;
import com.pk.platform.domain.question.QuestionKey;
import com.pk.platform.util.StringConverter;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.constant.QuestionConstant;
import com.pk.platform.util.page.Pager;

public class PaperUseServiceImpl implements IPaperUseService {

	private IPaperUseDao paperUseDao;
	
	private IPaperDefineDao paperDefineDao;
	
	public List<Map<String, Object>> queryPaperFixByPage(PaperVO pvo, Pager pager)
	{
		return paperUseDao.queryPaperFixByPage(pvo, pager);
	}

	public synchronized Map<String, Object> addPaperFix(PaperFix paperFix)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String result = "result";
		boolean flag = paperUseDao.existPaperFixCode(paperFix.getPfCode(), paperFix.getPaper().getId());
		if(flag == true)
		{
			resultMap.put(result, "hasCode");
			return resultMap;
		}
		else
		{
			paperUseDao.add(paperFix);
			resultMap.put(result, "");
			resultMap.put("id", paperFix.getId());
		}
		return resultMap;
	}
	
	public String updatePaperFix(PaperFix paperFix, Map<String, Object>[] answerList)
	{
		PaperFix pf = this.queryPaperFixById(paperFix.getId());
		pf.setPfName(paperFix.getPfName());
		pf.setPfSex(paperFix.getPfSex());
		pf.setSaveFlag(Constant.Y);
		paperUseDao.delQuestionKeyByPaperFix(paperFix.getId());
		paperUseDao.getHibernateSession().flush();
		for(Map<String, Object> answer : answerList)
		{
			String id = answer.get("id") == null ? "" : answer.get("id").toString();
			String key = answer.get("val") == null ? "" : answer.get("val").toString();
			String fitType = answer.get("fitType") == null ? "" : answer.get("fitType").toString();
			if(!StringConverter.isNotNull(key))
			{
				continue;
			}
			String[] names = id.split("_");
			String qesCode = names.length >= 2 ? names[1] : "";
			Question qes = paperDefineDao.queryQuestionByCode(qesCode);
			QuestionKey qk = new QuestionKey();
			qk.setPaperFix(pf);
			qk.setTxtId(id);
			qk.setQesKey(key);
			qk.setFitType(fitType);
			qk.setQuestion(qes);
			paperUseDao.add(qk);
		}
		paperUseDao.getHibernateSession().flush();
		String jsonKey = this.queryQuestionKeyListJson(paperFix.getId());
		return jsonKey;
	}
	
	public void delPaperFix(String id)
	{
		paperUseDao.delete(PaperFix.class, id);
	}
	
	public PaperFix queryPaperFixById(String id)
	{
		return paperUseDao.get(PaperFix.class, id);
	}
	
	public List<Map<String, Object>> queryQuestionListByPaper(String paperId)
	{
		List<Map<String, Object>> qesList = paperUseDao.queryQuestionListByPaper(paperId);
		for(Map<String, Object> qes : qesList)
		{
			//组题
			if(QuestionConstant.GROUP_SINGLE.equals(qes.get(QuestionConstant.QUESTION_TYPE).toString()) ||
				QuestionConstant.GROUP_NUMERIC.equals(qes.get(QuestionConstant.QUESTION_TYPE).toString()) ||
				QuestionConstant.GROUP_MULTIPLE.equals(qes.get(QuestionConstant.QUESTION_TYPE).toString()))
			{
				String groupQes = qes.get("groupQes") == null ? "" : qes.get("groupQes").toString();
				List<Map<String, Object>> groupQesList = paperUseDao.queryQuestionItemOption(groupQes);
				qes.put("groupQesList", groupQesList);
			}
			//选项
			if(QuestionConstant.SINGLE_CHOICE.equals(qes.get(QuestionConstant.QUESTION_TYPE).toString()) ||
				QuestionConstant.MULTIPLE_CHOICE.equals(qes.get(QuestionConstant.QUESTION_TYPE).toString()) ||
				QuestionConstant.GROUP_SINGLE.equals(qes.get(QuestionConstant.QUESTION_TYPE).toString()) ||
				QuestionConstant.GROUP_MULTIPLE.equals(qes.get(QuestionConstant.QUESTION_TYPE).toString()))
			{
				String ctId = qes.get("ctId") == null ? "" : qes.get("ctId").toString();
				Map<String, Object> codeTable = paperUseDao.queryOneCodeTable(ctId);
				List<Map<String, Object>> optionList = paperUseDao.queryQuestionItemOption(ctId);
				qes.put("codeTable", codeTable);
				qes.put("optionList", optionList);
			}
		}
		return qesList;
	}
	
	public String queryPaperJson(String paperId)
	{
		Paper paper = paperUseDao.get(Paper.class, paperId);
		StringBuffer json = new StringBuffer();
		json.append("[{'halfQesRow':").append(paper.getHalfQesRow());
		json.append(", 'fullQesRow':").append(paper.getFullQesRow()).append("}]");
		return json.toString();
	}
	
	public String queryQuestionKeyListJson(String pfId)
	{
		List<Map<String, Object>> list = paperUseDao.queryQuestionKeyList(pfId);
		String json = JSONArray.fromObject(list).toString();
		return json;
	}
	
	public String queryAllPaperFixKeyJson(String paperId)
	{
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Paper paper = paperDefineDao.get(Paper.class, paperId);
		Set<PaperFix> pfSet = paper.getPaperFixSet();
		for(PaperFix pf : pfSet)
		{
			if(Constant.N.equals(pf.getSaveFlag()))
				continue;
			List<Map<String, Object>> keyList = paperUseDao.queryQuestionKeyList(pf.getId());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pfId", pf.getId());
			map.put("pfCode", pf.getPfCode());
			map.put("key", keyList);
			list.add(map);
		}
		String resultJson = JSONArray.fromObject(list).toString();
		return resultJson;
	}
	
	public IPaperUseDao getPaperUseDao() {
		return paperUseDao;
	}

	public void setPaperUseDao(IPaperUseDao paperUseDao) {
		this.paperUseDao = paperUseDao;
	}

	public IPaperDefineDao getPaperDefineDao() {
		return paperDefineDao;
	}

	public void setPaperDefineDao(IPaperDefineDao paperDefineDao) {
		this.paperDefineDao = paperDefineDao;
	}
	
}
